package com.hugehard.here;

import java.util.ArrayList;

import android.app.ActionBar;
import android.app.ActionBar.OnNavigationListener;
import android.content.Context;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SearchView;
import android.widget.SearchView.OnCloseListener;
import android.widget.SpinnerAdapter;
import android.widget.TextView;

import com.hugehard.here.ContactsManager.BaseContactItem;
import com.hugehard.here.ContactsManager.CallLogItem;
import com.hugehard.here.ContactsManager.ContactItem;

public class ContactsActivity extends BaseActivity implements SearchView.OnQueryTextListener{
	private final static String TAG = ContactsActivity.class.getSimpleName();

	private boolean mbIsTabStarsSelected = true;
	private RecentContactView mRecentContactView = null;
	private AllContactView mAllContactView = null;
	private SearchResultsView mSearchResultsView = null;
	
	private SearchView mSearchView = null;
	private LinearLayout mLayout = null;
	private LayoutInflater mInflater = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.contacts_activity);

		mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
		ActionBar actionBar = getActionBar();
		actionBar.setDisplayShowTitleEnabled(false);
		actionBar.setDisplayHomeAsUpEnabled(true);
		actionBar.setHomeButtonEnabled(true);
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
		
		ArrayList<NavigationItem> items = new ArrayList<NavigationItem>();
		items.add(new NavigationItem(R.string.tab_stars, R.drawable.navigation_drop_down_icon_star));
		items.add(new NavigationItem(R.string.tab_all, R.drawable.navigation_drop_down_icon_all));
		
		NavigationAdapter spinnerAdapter = new NavigationAdapter(items);
		actionBar.setListNavigationCallbacks(spinnerAdapter,
				mOnNavigationListener);

		mRecentContactView = new RecentContactView(this);
		mAllContactView = new AllContactView(this);
		mLayout = (LinearLayout) findViewById(R.id.contactListView);
		if (mbIsTabStarsSelected) {
			mLayout.addView(mRecentContactView.getView(),
					android.view.ViewGroup.LayoutParams.MATCH_PARENT, android.view.ViewGroup.LayoutParams.MATCH_PARENT);
		} else {
			mLayout.addView(mAllContactView.getView(),
					android.view.ViewGroup.LayoutParams.MATCH_PARENT, android.view.ViewGroup.LayoutParams.MATCH_PARENT);
		}
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.searchview_in_menu, menu);
        MenuItem searchItem = menu.findItem(R.id.action_search);
        mSearchView = (SearchView) searchItem.getActionView();
        mSearchView.setOnQueryTextListener(this);
        
        mSearchView.setOnSearchClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				ArrayList<BaseContactItem> items = new ArrayList<BaseContactItem>();
				if (mbIsTabStarsSelected) {
					ArrayList<CallLogItem> callLogItems = ContactsManager.getInstance().getRecentContactItemList();
					for (CallLogItem item: callLogItems) {
						items.add(item);
					}
				} else {
					ArrayList<ContactItem> contactItems = ContactsManager.getInstance().getAllContactItemList();
					for (ContactItem item: contactItems) {
						items.add(item);
					}
				}
				mSearchResultsView = new SearchResultsView(ContactsActivity.this, items);
				mLayout.removeAllViews();
				mLayout.addView(mSearchResultsView.getView(),
						android.view.ViewGroup.LayoutParams.MATCH_PARENT, android.view.ViewGroup.LayoutParams.MATCH_PARENT);
			}
		});
        
        mSearchView.setOnCloseListener(new OnCloseListener() {
			@Override
			public boolean onClose() {
				mLayout.removeAllViews();
				if (mbIsTabStarsSelected) {
					mLayout.addView(mRecentContactView.getView(),
							android.view.ViewGroup.LayoutParams.MATCH_PARENT, android.view.ViewGroup.LayoutParams.MATCH_PARENT);
				} else {
					mLayout.addView(mAllContactView.getView(),
							android.view.ViewGroup.LayoutParams.MATCH_PARENT, android.view.ViewGroup.LayoutParams.MATCH_PARENT);
				}
				return false;
			}
		});
		return super.onCreateOptionsMenu(menu);
	}
	

	private OnNavigationListener mOnNavigationListener = new OnNavigationListener() {
		@Override
		public boolean onNavigationItemSelected(int position, long itemId) {
			if (position == 0) {
				if (mbIsTabStarsSelected == false) {
					mbIsTabStarsSelected = true;
					mLayout.removeAllViews();
					mLayout.addView(mRecentContactView.getView(),
							android.view.ViewGroup.LayoutParams.MATCH_PARENT,
							android.view.ViewGroup.LayoutParams.MATCH_PARENT);
				}
			} else if (position == 1) {
				if (mbIsTabStarsSelected) {
					mbIsTabStarsSelected = false;
					mLayout.removeAllViews();
					mLayout.addView(mAllContactView.getView(),
							android.view.ViewGroup.LayoutParams.MATCH_PARENT,
							android.view.ViewGroup.LayoutParams.MATCH_PARENT);
				}
			}
			return true;
		}
	};
	
	
	private class NavigationItem {
		public int mStringResourceId = 0;
		public int mIconResourceId = 0;
		public NavigationItem(int strId, int resourceId) {
			mStringResourceId = strId;
			mIconResourceId = resourceId;
		}
	}
	
	private class NavigationAdapter implements SpinnerAdapter {
		private ArrayList<NavigationItem> mItems = new ArrayList<NavigationItem>();
		
		class ViewHolder {
			public TextView mTv = null;
			public ImageView mIv= null;
		}
		
		public NavigationAdapter(ArrayList<NavigationItem> items) {
			mItems = items;
		}

		@Override
		public int getCount() {
			return mItems.size();
		}

		@Override
		public NavigationItem getItem(int position) {
			return mItems.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public int getItemViewType(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder viewHolder = null;
			if (convertView == null) {
				convertView = mInflater.inflate(R.layout.action_bar_navigation_item, parent, false);
				viewHolder = new ViewHolder();
				viewHolder.mIv = (ImageView) convertView.findViewById(R.id.navigation_item_icon);
				viewHolder.mTv = (TextView) convertView.findViewById(R.id.navigation_item_text);
				convertView.setTag(viewHolder);
			} else {
				viewHolder = (ViewHolder) convertView.getTag();
			}
			
			NavigationItem item = getItem(position);
			if (item != null) {
				viewHolder.mTv.setText(item.mStringResourceId);
				viewHolder.mTv.setTextColor(getResources().getColor(R.color.white));
				viewHolder.mIv.setImageResource(item.mIconResourceId);
			}
			return convertView;
		}

		@Override
		public int getViewTypeCount() {
			return 1;
		}

		@Override
		public boolean hasStableIds() {
			return false;
		}

		@Override
		public boolean isEmpty() {
			return mItems.isEmpty();
		}

		@Override
		public void registerDataSetObserver(DataSetObserver observer) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void unregisterDataSetObserver(DataSetObserver observer) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public View getDropDownView(int position, View convertView,
				ViewGroup parent) {
			return getView(position, convertView, parent);
		}
		
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem menuItem) {
		if (menuItem.getItemId() == android.R.id.home) {
			goToMainActivity();
			return true;
		}
		return super.onOptionsItemSelected(menuItem);
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			goToMainActivity();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public boolean onQueryTextChange(String newText) {
		if (mSearchResultsView != null) {
			mSearchResultsView.onSearchKeyWordChanged(mSearchView.getQuery().toString());
		}
		return false;
	}

	@Override
	public boolean onQueryTextSubmit(String query) {
		if (mSearchResultsView != null) {
			mSearchResultsView.onSearchKeyWordChanged(query);
		}
		return false;
	}
}